package cn.yitun.chat.yitunservermanagecore.general.manage.common.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.annotation.Log;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.converter.dto2po.common.DictTypeConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.common.DictTypeDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.common.DictTypeVo;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.common.service.DictTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型表
 *
 * @author admin
 * @email chen@163.com
 * @date 2019-02-15 09:16:13
 */

@RestController
@RequestMapping("/manage/common")
public class DictTypeController extends BaseController {
    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 类型info
     */
    @RequestParameter
    @PermissionMapping(name = "字典类型", perms = "common:dictType:dictType")
    @GetMapping("/dictType/detail/{dictId}")
    public ResultMessage detail(@PathVariable("dictId") Long dictId) {
        ResultMessage rm = new ResultMessage();
        rm.bodyPut("info", dictTypeService.getVO(dictId));
        return  rm;
    }

    @RequestParameter
    @PermissionMapping(name = "字典类型分页", perms = "common:dictType:dictType")
    @PostMapping("/dictType/page")
    public ResultMessage page(JwtUserDto jwtUserDto, @JsonDefine DictTypeDto dictTypeDto) {
        //查询列表数据
        checkRootPage(dictTypeDto, jwtUserDto);
        PageHelper.startPage(dictTypeDto.getCurrentPageNo() , dictTypeDto.getPageSize());
        List<DictTypeVo> dataVos = dictTypeService.listVO(dictTypeDto);
        ResultMessage rm = new ResultMessage();
        rm.bodyPut("page", new PageInfo<>(dataVos));
        return rm;
    }
    @RequestParameter
    @PermissionMapping(name = "字典类型列表", perms = "common:dictType:dictType")
    @PostMapping("/dictType/list")
    public ResultMessage list(JwtUserDto jwtUserDto, @JsonDefine DictTypeDto dictTypeDto) {
        checkRootPage(dictTypeDto, jwtUserDto);
        ResultMessage rm = new ResultMessage();
        rm.bodyPut("list", dictTypeService.listVO(dictTypeDto));
        return rm;
    }

    /**
     * 保存
     */
    @Log("字典类型新增")
    @RequestParameter
    @PermissionMapping(name = "字典类型新增", perms = "common:dictType:dictType")
    @PostMapping("/dictType/save")
    public ResultMessage save(JwtUserDto jwtUserDto, @JsonDefine DictTypeDto dictTypeDto) {
        setUserMsgSave(dictTypeDto, jwtUserDto);
        if (!dictTypeService.checkDictTypeUnique( DictTypeConverter.INSTANCE.dto2do(dictTypeDto) ))
            throw new YitunException(ExceptionEnum.DICT_TYPE_EXIST);
        if (dictTypeService.save(dictTypeDto) == 0)
            throw new YitunException(ExceptionEnum.ADD_FAIL);
        return  new ResultMessage();
    }

    /**
     * 修改
     */
    @Log("字典类型编辑")
    @RequestParameter
    @PermissionMapping(name = "字典类型编辑", perms = "common:dictType:dictType")
    @PostMapping("/dictType/update")
    public ResultMessage update(JwtUserDto jwtUserDto, @JsonDefine DictTypeDto dictTypeDto) {
        if (dictTypeDto.getId() == null )
            throw new YitunException(ExceptionEnum.REQUEST_PARAM_NULL);

        if (!dictTypeService.checkDictTypeUnique( DictTypeConverter.INSTANCE.dto2do(dictTypeDto) ))
            throw new YitunException(ExceptionEnum.DICT_TYPE_EXIST);

        setUserMsgUpdate(dictTypeDto, jwtUserDto);
        if (dictTypeService.update(dictTypeDto) == 0)
            throw new YitunException(ExceptionEnum.EDIT_FAIL);
        return  new ResultMessage();
    }

    /**
     * 删除
     */
    @Log("字典类型删除")
    @RequestParameter
    @PermissionMapping(name = "字典类型删除", perms = "common:dictType:dictType")
    @DeleteMapping("/dictType/remove/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        if (dictTypeService.remove(id) == 0)
            throw new YitunException(ExceptionEnum.DELETE_FAILED);
        return  new ResultMessage();
    }


    //------------------------旧版本---------------------------


    /**
     * 批量删除
     */
    @Log("字典类型批量删除")
    @RequestParameter
    @PermissionMapping(name = "字典类型批量删除", perms = "common:dictType:dictType")
    @PostMapping("/dictType/batchRemove/{ids}")
    public ResultMessage batchRemove(@PathVariable("ids") Long[] ids) {
        if (dictTypeService.batchRemove(ids) == 0)
            throw new YitunException(ExceptionEnum.DELETE_FAILED);
        return  new ResultMessage();
    }



    /**
     * 校验字典类型
     */
    @RequestParameter
    @PermissionMapping(name = "校验字典类型", perms = "common:dictType:dictType")
    @PostMapping("/dictType/checkDictTypeUnique")
    public ResultMessage checkDictTypeUnique(@JsonDefine DictTypeDto dictTypeDto) {
        if( !dictTypeService.checkDictTypeUnique(DictTypeConverter.INSTANCE.dto2do(dictTypeDto) ))
            throw new YitunException(ExceptionEnum.DICT_TYPE_EXIST);
        return new ResultMessage();
    }


}
